define(function (require,exports,module) {
    var myPub=require('./public');


    var oContain = $('.js_contain');
    var oGoTop = $('.js_goTop');                    // 回到顶部
    var oSideBox = $('.js_sideBox');		        // 侧边按钮框
    var oContain = $('.js_contain');
    var oGoodStoreChoice = $('.js_goodStoreChoice');

    var oGoodsList = $('.js_supllierList');         //每日好店列表的盒子
    var oLoadBtn = $('.js_loadingBtn');            //加载更多


    var goodStoreUrl = '/supplier/choice/list';
    var storeDetailUrl = '/m/g/supplier/1.0/isOverSeaSupplier';        // 判断是进入海外店铺还是进入普通店铺的接口


    // 回到过顶部
    oGoTop.hammer().on('tap', function () {
        oContain.eq(0).stop().animate({scrollTop: '0'}, 250);

    });



    module.exports={
        init:function () {
            supplier.initEvent();
            supplierData.getSuppplierData();
            supplier.loadMoreStore();
        }
    };



    var supplier={
        time_Scroll: null,
        initEvent: function () {
            // lazLoading.AddLazLoadingImg(oGoodsList[0]);
            // 导航选中设置
            // oFooter.find('li').eq(3).addClass('active');
            oSideBox.stop().animate({'right': '-3.5rem'}, 300);

        },

        loadMoreStore: function () {
          var _this = this;

            var oContainScroll = $('.js_contain');
            var minScrHeight = 300;
            oContain.off('scroll');
            oContain.on('scroll',function (e) {
                var afterScroll = $(this).scrollTop();
                // console.log('hhhhaaa',afterScroll);
                //返回顶部
                _this.ShowGoTop(afterScroll);
                clearTimeout(_this.time_Scroll);
                _this.time_Scroll = setTimeout(function () {


                    if(oContainScroll[0].scrollHeight - afterScroll - oContainScroll.height() < minScrHeight){
                        // console.log(';;;;;',supplierData.loadComplete,supplierData.isLoading)
                        //如果没加载完成就继续加载
                        if(!supplierData.loadComplete){
                            //如果不是正在加载中的话就加载
                            if(!supplierData.isLoading){
                                //更新请求开始的索引
                                supplierData.start = supplierData.start + supplierData.limit;
                                supplierData.getSuppplierData();
                            }

                        }
                    }
                },150)
            })

        },

        // 显示回到顶部按钮
        ShowGoTop: function (scrollTop) {
            scrollTop > 300 ? oSideBox.stop().animate({'right': 0}, 300) : oSideBox.stop().animate({'right': '-3.5rem'}, 300);
        },

        // 点击进入店铺详情页面要判断是否是海外店铺
        goToStoreEvent: function(){
            $('.js_goToStore').on('click',function () {
                var supplierId = $(this).attr('data-supplierId');
                supplierData.goToStoreDetail(supplierId);
            })
        }
    };
    
    var supplierData = {
        start: 0,
        limit: 5,
        firstLoad: true,
        isLoading: false,
        loadComplete: false,
        //获取每日好店的列表
        getSuppplierData: function () {
            var _this = this;

            //判断是否正在加载
            if(!_this.isLoading){
                _this.isLoading = true;
                G_JSON_POST(goodStoreUrl,{start: _this.start, limit: _this.limit},function (res) {
                    // console.log('j------',res);
                    var result = res;
                    _this.isLoading = false;
                    //判断请求是否返回成功
                    if(result.status == 'success'){

                        //判断返回有没有数据
                        if(result.obj){
                            _this.firstLoad = false;
                            //判断请求回来的数据是否等于最大请求量，否则认为加载完成。
                            if(result.obj.length >= _this.limit){
                                //显示正在加载中
                                oLoadBtn.hide().eq(1).show();
                            }else {
                                //加载完成
                                _this.loadComplete = true;
                                //显示没有更多
                                oLoadBtn.hide().eq(0).show();
                            }
                            //插入好店列表
                            oGoodsList.append(supplierHtml.storeListHtml(result.obj));
                            // 执行判断店铺页面链接的方法
                            supplier.goToStoreEvent();
                        }
                        else {
                            //是否是第一次加载，如果是第一次加载但没返回数据则显示空白页
                            if(_this.firstLoad){
                                oContain.append(supplierHtml.wrongPageHtml());
                                oGoodStoreChoice.hide();
                            }
                            //不是第一次加载，就是滚动加载，但没有数据，则认为加载完成，且显示没有更多
                            else {
                                _this.loadComplete = true;
                                oLoadBtn.hide().eq(0).show();
                                return;
                            }
                        }
                    }else {
                        //如果是第一次加载并加载不成功，则显示空白页
                        if(_this.firstLoad){
                            oContain.append(supplierHtml.wrongPageHtml());
                            oGoodStoreChoice.hide();
                        }
                        else {
                            return;
                        }
                    }



                },function (err) {

                })
            }

        },

        //截取图片方法
        cutImagePath: function (data) {
            var newArr = [];
            newArr = data.split(',');

            return newArr[0];
        },

        // 判断是进入海外店铺还是进入普通店铺的接口
        goToStoreDetail: function (supplierId) {
            var _this = this;
            G_JSON_POST(storeDetailUrl,{'supplierId':supplierId},function (data) {
                var _data = data.data;
                var _ret = data.ret;
                if(_ret.code=='0'){
                    if(_data){
                        // 海外馆商家
                        window.location.href =  BASE +'/h/store/storehome?supplierId='+ supplierId ;
                    }
                    else{
                        // 普通商家
                        window.location.href =  BASE +'/h/store/normalStore/'+ supplierId ;
                    }
                }
                else{
                    // 如果请求不到就跳转普通商家
                    window.location.href =  BASE +'/h/store/normalStore/'+ supplierId ;
                }
            })
        },

    };


    var supplierHtml = {
        //每日好店的列表
        storeListHtml: function (data) {
            var _this = this;
            var shtml = '';
            for(var i = 0; i < data.length; i++){
                var item = data[i];
                var star = parseInt(item.star * 1);
                var headPortImg = item.headPortraitPath ? HTTP_IMAGE_URL + item.headPortraitPath : BASE + '/static_v4/image/sfddj.jpg';
                shtml +=  '<li>'+
                          '    <div class="goodStoreBox">'+
                          _this.storeGoodsHtml(item.goodsList)+
                          '       <a class="goodStoreMes HBox js_goToStore" data-supplierId="'+item.supplierId+'">'+
                          '          <div class="goodStoreMesLeft HBox Bflex1 BoxCenter">'+
                          '              <figure class="storeHeadImg">'+
                          '                  <img src="'+ headPortImg  +'" alt="">'+
                          '              </figure>'+
                          '              <aside class="goodStoreDes Bflex1 VBox">'+
                          '                  <h5 class="Bflex1">'+ item.nickName +'</h5>'+
                          '                  <div class="goodStoreScore HBox">'+
                          '                      <p class="goodStoreScoreTit">综合评分</p>'+
                          '                      <p class="HBox BoxCenter allStarBox'+ star +'">'+
                          '                          <span class="icon-star star1"></span>'+
                          '                          <span class="icon-star star2"></span>'+
                          '                          <span class="icon-star star3"></span>'+
                          '                          <span class="icon-star star4"></span>'+
                          '                          <span class="icon-star star5"></span>'+
                          '                      </p>'+
                          '                  </div>'+
                          '             </aside>'+
                          '            </div>'+
                          '            <div class="goodStoreMesRight HBox BoxCenter">'+
                          '                <div class="goodStoreEntance HBox BoxCenter">'+
                          '                   <p>进店</p>'+
                          '                   <span class=" icon-href-right"></span>'+
                          '                </div>'+
                          '           </div>'+
                          '       </a>'+
                          '    </div>'+
                          '</li>'
            }

            return shtml;
        },

        //判断是几个图
        storeGoodsHtml: function (data) {
            var shtml = '';
            var _this = this;

            var goodsList = data;
            if(goodsList) {
                if( goodsList.length == 1 ){
                    shtml = _this.oneImgHtml(goodsList);
                }
                else if( goodsList.length == 2 ){
                    shtml = _this.twoImgHtml(goodsList);
                }
                else {
                    shtml = _this.threeImgHtml(goodsList);
                }
            }

            return shtml;
        },

        //一个图的好店
        oneImgHtml: function (data) {
            var shtml = '';
            shtml =  '<a href="'+ BASE +'/h/goodspay/goodsView/'+ data[0].goodsSn +'" class="goodStoreImg goodStoreImg_1 HBox">'+
                                <!--左边图-->
                     '     <div class="leftGoodsImg Bflex1">'+
                     '          <figure>'+
                     '               <img src="'+ HTTP_IMAGE_URL + supplierData.cutImagePath(data[0].goodsImagePath) +'" alt="">'+
                     '          </figure>'+
                     '     </div>'+
                     ' </a>';

            return shtml;
        },

        //两个图的好店
        twoImgHtml: function (data) {
            var shtml = '';

            shtml =  '<section class="goodStoreImg goodStoreImg_2 HBox">'+
                     '     <a href="'+ BASE +'/h/goodspay/goodsView/'+ data[0].goodsSn +'" class="leftGoodsImg Bflex1">'+
                     '          <figure class="goodsStoreFigure">'+
                     '              <img src="'+ HTTP_IMAGE_URL + supplierData.cutImagePath(data[0].goodsImagePath) +'" alt="">'+
                     '           </figure>'+
                     '      </a>'+
                     '      <a href="'+ BASE +'/h/goodspay/goodsView/'+ data[1].goodsSn +'" class="rightGoodsImg Bflex1 VBox">'+
                     '           <figure class="Bflex1 goodsStoreFigure">'+
                     '               <img src="'+ HTTP_IMAGE_URL + supplierData.cutImagePath(data[1].goodsImagePath) +'" alt="">'+
                     '           </figure>'+
                     '       </a>'+
                     '</section>';

            return shtml;
        },

        //3个图的好店
        threeImgHtml: function (data) {
            var shtml = '';

            shtml =  '<section class="goodStoreImg goodStoreImg_3 HBox">'+
                     '    <div class="leftGoodsImg VBox">'+
                     '         <ul class="VBox Bflex1">'+
                     '              <li class="Bflex1 VBox">'+
                     '                   <a class="Bflex1 VBox" href="'+ BASE +'/h/goodspay/goodsView/'+ data[0].goodsSn +'">'+
                     '                       <figure >' +
                     '                          <img class="Bflex1" src="'+ HTTP_IMAGE_URL + supplierData.cutImagePath(data[0].goodsImagePath) +'" alt="">'+
                     '                       </figure>'+
                     '                   </a>'+
                     '               </li>'+
                     '          </ul>'+
                     '     </div>'+
                     '     <div class="rightGoodsImg Bflex1 VBox">'+
                     '          <ul class="VBox Bflex1">'+
                     '                <li class="Bflex1 posi_re">'+
                     '                     <a class="right_topImg" href="'+ BASE +'/h/goodspay/goodsView/'+ data[1].goodsSn +'">'+
                     '                              <img src="'+ HTTP_IMAGE_URL + supplierData.cutImagePath(data[1].goodsImagePath) +'" alt="">'+
                     '                     </a>'+
                     '                 </li>'+
                     '                 <li class="Bflex1 posi_re">'+
                     '                     <a class="right_topImg pad0" href="'+ BASE +'/h/goodspay/goodsView/'+ data[2].goodsSn +'">'+
                     '                              <img src="'+ HTTP_IMAGE_URL + supplierData.cutImagePath(data[2].goodsImagePath) +'" alt="">'+
                     '                     </a>'+
                     '                  </li>'+
                     '            </ul>'+
                     '       </div>'+
                     ' </section>';

            return shtml;
        },

        //空白页
        wrongPageHtml: function () {
            var shtml = '';

            shtml = '<div class="noContent VBox BoxCenter">'+
                    '     <figure class="noContent_image">'+
                    '          <img src="/static_v4/image/wonderful/loadingpage_nodata.png">'+
                    '     </figure>'+
                    '     <p class="noContent_text">页面暂无内容显示</p>'+
                    '</div>';

            return shtml;
        }

    }


});